<?php
	include'Utils.php';
	class CoursesController extends AppController{
		
		/**
		 * List components used in this class
		 */
		public $components = array('CenterInfo', 'Subject', 'SubCourse');
		
		/**
		 * Admin function used to view all courses
		 */
		public function manageListCourses() {
			
			// Check session and redirect to login page
			Utils::checkSession($this);
			$this->set('admin', '1');
			// Get common information
			$centerInfo = $this->CenterInfo->getInfo();
			$this->set('center_info', $centerInfo);
			// Get all courses
			$this->set("courses", $this->Course->find('all'));
			
		}
		
		/**
		 * Admin function used to display add course page
		 */
		public function add(){
			
			// Check session and redirect to login page
			Utils::checkSession($this);
			$this->set('admin', '1');
			
			// Get common information
			$centerInfo = $this->CenterInfo->getInfo();
			$this->set('center_info', $centerInfo);
			
			$this->set('subjects', $this->Subject->getListSubject());
			$this->set('name', isset($_POST['name']) ? htmlentities($_POST['name'], ENT_QUOTES, 'utf-8') : "");
			$this->set('descript', isset($_POST['descript']) ? htmlentities($_POST['descript'], ENT_QUOTES, 'utf-8') : "");
			$this->set('listSubs', isset($_POST['listSubs']) ? $_POST['listSubs'] : "");
			$this->set('money', isset($_POST['money']) ? $_POST['money'] : "");
			
		}
		
		public function confirmAdd(){
			
			// Check session and redirect to login page
			Utils::checkSession($this);
			$this->set('admin', '1');
			
			// Get common information
			$centerInfo = $this->CenterInfo->getInfo();
			$this->set('center_info', $centerInfo);
			
			// Get POST data
			$name = isset($_POST['name']) ? $_POST['name'] : "";
			$listSubStr = isset($_POST['listSubs']) ? $_POST['listSubs'] : "";
			$descript = isset($_POST['descript']) ? htmlentities($_POST['descript'], ENT_QUOTES, 'utf-8') : "";
			$money = isset($_POST['money']) ? $_POST['money'] : 0;
			
			$this->set('subjects', $this->Subject->getListByListId($listSubStr));
			$this->set('name', $name);
			$this->set('listSub', $listSubStr);
			$this->set('descript', $descript);
			$this->set('money', $money);
		}
		
		/**
		 * Admin function used to regist new course
		 */
		public function regist(){
			
			// Check session and redirect to login page
			Utils::checkSession($this);
			$this->set('admin', '1');
			
			// Get POST data
			$name = isset($_POST['name']) ? htmlentities($_POST['name'], ENT_QUOTES, 'utf-8') : "";
			$listSubStr = isset($_POST['listSubs']) ? $_POST['listSubs'] : "";
			$descript = isset($_POST['descript']) ? htmlentities($_POST['descript'], ENT_QUOTES, 'utf-8') : "";
			
			if($name == '' || $listSubStr == '' || $descript == ''){
				$this->redirect(array('action' => 'manageListCourses'));
			}
			
			$this->Course->create();
			
			$this->request->data['m_course_id'] = null;
			$this->request->data['m_course_name'] = $name;
			$this->request->data['m_course_descript'] = $descript;
			
			if($this->Course->save($this->request->data)){
				$id = $this->Course->id;
				$arrSubId = explode(',', $listSubStr);
				for($i = 0; $i < count($arrSubId); $i++){
					$this->SubCourse->regist($arrSubId[$i], $id);
				}
				$this->redirect(array('action' => 'manageListCourses'));
			}
			
		}
		
		/**
		 * Admin function used to display edit page
		 */
		public function edit(){
			
			// Check session and redirect to login page
			Utils::checkSession($this);
			$this->set('admin', '1');
			
			// Get common information
			$centerInfo = $this->CenterInfo->getInfo();
			$this->set('center_info', $centerInfo);
			// Get POST data
			$id = isset($_POST['id']) ? $_POST['id'] : "";
			if($id == ""){
				$this->redirect(array("action" => 'manageListCourses'));
			}
			
			$courses = $this->Course->find('all', array(
				'conditions' => array('Course.m_course_id' => $id)
			));
			$course = $courses[0];
			
			$name = isset($_POST['name']) ? $_POST['name'] : $course['Course']['m_course_name'];
			$descript = isset($_POST['descript']) ? htmlentities($_POST['descript'], ENT_QUOTES, 'utf-8') : htmlentities($course['Course']['m_course_descript'], ENT_QUOTES, 'utf-8');
			$strSubIds = isset($_POST['listSubs']) ? $_POST['listSubs'] : "";
			$money = isset($_POST['money']) ? $_POST['money'] : $course['Course']['m_course_money'];
			if($strSubIds == ""){
				$listSubs = $this->SubCourse->getListByCourseId($id);
				foreach($listSubs as $sub):
					if($strSubIds == ""){
						$strSubIds = $sub["SubCourse"]['f_sub_id'];
					}else{
						$strSubIds = $strSubIds . ",". $sub["SubCourse"]['f_sub_id'];
					}
				endforeach;
			}
			$this->set('oldName', $course['Course']['m_course_name']);
			$this->set('subjects', $this->Subject->getListSubject());
			$this->set('id', $id);
			$this->set('name', $name);
			$this->set('descript', $descript);
			$this->set('money', $money);
			$this->set('listSubs', $strSubIds);
			
		}
		
		/**
		 * Admin function used to display confirm edit page
		 */
		public function confirmEdit(){
			
			// Check session and redirect to login page
			Utils::checkSession($this);
			$this->set('admin', '1');
			
			// Get common information
			$centerInfo = $this->CenterInfo->getInfo();
			$this->set('center_info', $centerInfo);
			
			// Get POST data
			$id = isset($_POST['id']) ? $_POST['id'] : "";
			if($id == ""){
				$this->redirect(array('action' => 'manageListCourses'));
			}
			
			$name = isset($_POST['name']) ? htmlentities($_POST['name'], ENT_QUOTES, 'utf-8') : "";
			$descript = isset($_POST['descript']) ? htmlentities($_POST['descript'], ENT_QUOTES, 'utf-8') : "";
			$money = isset($_POST['money']) ? $_POST['money'] : 0;
			$strSubIds = isset($_POST['listSubs']) ? $_POST['listSubs'] : "";
			
			// Set displayed data
			$this->set('id', $id);
			$this->set('subjects', $this->Subject->getListByListId($strSubIds));
			$this->set('name', $name);
			$this->set('descript', $descript);
			$this->set('money', $money);
			$this->set('listSubs', $strSubIds);
		}
		
		/**
		 * Admin function used to display edit Course
		 */
		public function registEdit(){
			
			// Check session and redirect to login page
			Utils::checkSession($this);
			$this->set('admin', '1');
			
			// Get POST data
			$id = isset($_POST['id']) ? $_POST['id'] : "";
			if($id == ""){
				$this->redirect(array(
					'action' => 'manageListCourses'
				));
			}
			
			$name = isset($_POST['name']) ? htmlentities($_POST['name'], ENT_QUOTES, 'utf-8') : "";
			$descript = isset($_POST['descript']) ? htmlentities($_POST['descript'], ENT_QUOTES, 'utf-8') : "";
			$money = isset($_POST['money']) ? $_POST['money'] : 0;
			$listSubs = isset($_POST['listSubs']) ? $_POST['listSubs'] : "";
			
			$data = array(
				'Course.m_course_name' => "'" . $name . "'",
				'Course.m_course_descript' => "'" . $descript . "'",
				'Course.m_course_money' => $money
			);
			
			$condition = array(
				'Course.m_course_id' => $id
			);
			
			if($this->Course->updateAll($data, $condition)){
				$this->SubCourse->deleteByCourse($id);
				$arrSubId = explode(',', $listSubs);
				for($i = 0; $i < count($arrSubId); $i++){
					$this->SubCourse->regist($arrSubId[$i], $id);
				}
				$this->redirect(array('action' => 'manageListCourses'));
			}
		}
		
		/**
		 * Admin function used to delete course
		 */
		public function delete(){
			
			// Check session and redirect to login page
			Utils::checkSession($this);
			$this->set('admin', '1');
			
			// Get POST data
			$id = isset($_POST['id']) ? $_POST['id'] : "";
			if($id == ""){
				$this->redirect(array('action' => 'manageListCourses'));
			}
			
			// Delete subject course
			$this->SubCourse->deleteByCourse($id);
			
			// Delete course
			$this->Course->deleteAll(array('Course.m_course_id' => $id), false);
			$this->redirect(array('action' => 'manageListCourses'));
			
		}
		
		/**
		 * Front-end function used to display course details
		 */
		public function details(){
			
			// Get common information
			$centerInfo = $this->CenterInfo->getInfo();
			$this->set('center_info', $centerInfo);
			
			$subjects = $this->Subject->getListSubject();
			$this->set('subjects', $subjects);
			
			// Get POST data
			$id = isset($_GET['id']) ? $_GET['id'] : "";
			if($id == ""){
				$this->redirect(array(
					'controller' => 'Pages',
					'action' => 'index'
				));
				
			}
			
			// Get all courses
			$listCourses = $this->Course->find('all');
			
			$courses = $this->Course->find('all', array(
				'conditions' => array(
					'Course.m_course_id' => $id
				) 
			));
			
			// Get list subject 
			$idList = "";
			$listSubIds = $this->SubCourse->getListByCourseId($id);
			foreach ($listSubIds As $subId){
				if($idList == ""){
					$idList = $subId['SubCourse']['f_sub_id'];
				}else{
					$idList = $idList . ',' . $subId['SubCourse']['f_sub_id'];
				}
			}

			$listSubs = $this->Subject->getListByListId($idList);
			$this->set('courseSubs', $listSubs);
			$this->set('course', $courses[0]);
			$this->set('courses', $listCourses);
			$this->set('selectedId', $id);
		}
		
		/**
		 * Function checkExist Course name
		 */
		public function checkExistName(){
			
			// Get POST data
			$name = isset($_POST['name']) ? $_POST['name'] : "";
			$type = isset($_POST['type']) ? $_POST['type'] : "";
			
			// Check exist
			if ($name != ""){
				if($type == 'add'){
					$count = $this->Course->find('count', array(
						'conditions' => array('Course.m_course_name' => $name)
					));
					
					if ($count > 0){
						echo "(Tên khóa học đã tồn tại.)"; die;
					}else{
						echo ""; die;
					}
				}else{
					$oldName = isset($_POST['oldName']) ? htmlentities($_POST['oldName'], ENT_QUOTES, 'utf-8') : "";
					if($name != $oldName){
						$count = $this->Course->find('count', array(
							'conditions' => array('Course.m_course_name' => $name)
						));
						if ($count > 0){
							echo "(Tên khóa học đã tồn tại.)"; die;
						}else{
							echo ""; die;
						}
					}else{
						echo ""; die;
					}
				}
			}else{
				echo "(Tên môn học không hợp lệ)"; die;
			}
			
		}
	}
?>